﻿using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;

    public float forwardForce = 2000f;
    public float sidewaysForce = 500f;

    // Start is called before the first frame update
    // void Start()
    // {
    //     // Debug.Log("Hello,World!");
    //     rb.AddForce(0, 200, 500);
    // }

    // Update is called once per frame
    // We marked this as "Fixed"Update because we are using it to mess with physics. 
    void FixedUpdate()
    {
        // Add a fordword force
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);

        if(Input.GetKey("d"))
        {
            // ForceMode.VelocityChange，向此刚体添加瞬时速度变化,忽略其质量。
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
        if(Input.GetKey("a")){
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }

        if(rb.position.y < -1f)
        {
            FindObjectOfType<GameManager>().EndGame();
        }

    }
}
